<template>
  <div class="change_container">
    <div class="change_box">
      <el-form :model="changeForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="原密码" prop="pass">
          <el-input type="old_password" v-model="changeForm.old_password" show-password autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="changeForm.password" show-password autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="changeForm.checkPass" show-password autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="change">提交</el-button>
          <el-button type="success" @click="resetForm">重置</el-button>
          <el-button type="info" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.changeForm.checkPass !== '') {
          this.$refs.changeForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.changeForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      changeForm: {
        username: '',
        old_password: '',
        password: '',
        checkPass: ''
      },
      rules: {
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      this.$refs.ruleForm.resetFields()
    },
    back () {
      this.$router.go(-1)
    },
    change () {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.post('/user/changePwd', {
        username: this.changeForm.username,
        old_password: this.changeForm.old_password,
        password: this.changeForm.password
      }).then(response => {
        if (response.data.code === 200) this.$message.success('修改成功')
        else this.$message.error('修改失败,请检查原密码是否正确!')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.change_container{
    height: 100%;
    background-color: rgb(236, 236, 236);
}
.change_box{
    width: 450px;
    height: 280px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}
.btns{
    display: flex;
    justify-content: flex-end;
}
.demo-ruleForm{
    position: absolute;
    width: 100%;
    top: 20px;
    padding: 0 20px;
    box-sizing: border-box;
}
</style>
